{extend name="public/common"}

{block name="style"}
<title>加班申请审批</title>
<link rel="stylesheet" href="/home/css/approval/add.css?v={$Think.config.version}">
{/block}

{block name="body"}
<div id="app" v-cloak>
    <div class="type-title">加班申请审批</div>
    <div class="form" id="overtime">
        <ul>
            <li>
                <p>类型：</p>
                <div class="box">加班申请</div>
            </li>
            <li>
                <p>审批类型：</p>
                <mt-radio class="type-radio" v-model="overtimeType" :options="options"></mt-radio>
            </li>
            <li>
                <p>开始时间：</p>
                <div @click="chooseStartTime" v-if="typeof startTime === 'number'" class="box">{{startTime | timeToString('ymdhi')}}</div>
                <div @click="chooseStartTime" v-else class="special box">{{startTime}}</div>
                <mt-datetime-picker
                        v-model="start_time"
                        type="datetime"
                        ref="timePicker_1"
                        year-format="{value} 年"
                        month-format="{value} 月"
                        date-format="{value} 日"
                        hour-format="{value} 时"
                        minute-format="{value} 分"
                        :start-date="nowTime"
                        :end-date="temp_time"
                        @confirm="startTimeConfirm">
                </mt-datetime-picker>
            </li>
            <li>
                <p>结束时间：</p>
                <div @click="chooseEndTime" v-if="typeof endTime === 'number'" class="box">{{endTime | timeToString('ymdhi')}}</div>
                <div @click="chooseEndTime" v-else class="special box">{{endTime}}</div>
                <mt-datetime-picker
                        v-model="end_time"
                        type="datetime"
                        ref="timePicker_2"
                        year-format="{value} 年"
                        month-format="{value} 月"
                        date-format="{value} 日"
                        hour-format="{value} 时"
                        minute-format="{value} 分"
                        :start-date="start_time"
                        @confirm="endTimeConfirm">
                </mt-datetime-picker>
            </li>
            <li>
                <p>时长（天）：</p>
                <div @click="toggleDuring" :class="during.nowType.id == 0 ? 'special box' : 'box'">{{during.nowType.name}}</div>
                <mt-popup  position="bottom"  v-model="during.popupVisible" class="mint-popup">
                    <div class="picker-toolbar">
                        <span class="mint-datetime-action mint-datetime-cancel" @click="toggleDuring">取消</span>
                        <span class="mint-datetime-action mint-datetime-confirm" @click="duringConfirm">确定</span>
                    </div>
                    <mt-picker :slots="during.slots" value-key="name" @change="selectDuring"></mt-picker>
                </mt-popup>
            </li>
            <li>
                <p>审批人：</p>
                <div @click="toggleApprover" :class="approver.nowType.userid == 0 ? 'special box' : 'box'">{{approver.nowType.username}}</div>
                <mt-popup  position="bottom"  v-model="approver.popupVisible" class="mint-popup">
                    <div class="picker-toolbar">
                        <span class="mint-datetime-action mint-datetime-cancel" @click="toggleApprover">取消</span>
                        <span class="mint-datetime-action mint-datetime-confirm" @click="approverConfirm">确定</span>
                    </div>
                    <mt-picker :slots="approver.slots" value-key="username" @change="selectApprover"></mt-picker>
                </mt-popup>
            </li>
            <li>
                <p>加班理由：</p>
                <div class="textarea">
                    <textarea type="text" v-model="remark" placeholder="请输入加班理由" oninput="if(value.length>200)value=value.slice(0,200);"></textarea>
                    <span>{{remark.length}} / 200字</span>
                </div>
            </li>
        </ul>
    </div>
    <div class="foot">
        <div class="cancel" @click="cancel">取消</div>
        <div class="confirm" @click="subimtAjax">确定</div>
    </div>
</div>
{/block}

{block name="script"}
<script>
    var vw = new Vue({
        el: "#app",
        data: {
            type: 5,
            startTime: '请选择开始时间',
            endTime: '请选择结束时间',
            start_time: new Date(),
            temp_time: new Date(new Date().getTime() + 365*24*3600*1000),
            end_time: new Date(),
            nowTime: new Date(),
            during: {
                slots: [
                    {
                        values: [],
                        defaultIndex: 1
                    }
                ],
                nowType: {
                    name: '请选择时长',
                    id: 0
                },
                onChangeType: {
                    name: '请选择时长',
                    id: 0
                },
                popupVisible:false
            },
            agent: '',
            approver: {
                slots: [
                    {
                        values: [],
                        defaultIndex: 1
                    }
                ],
                nowType: {
                    username: '请选择审批人',
                    userid: 0
                },
                onChangeType: {
                    username: '请选择审批人',
                    userid: 0
                },
                popupVisible: false
            },
            remark: '',
            id:0,
            approvalUser: [],
            clickLoading: true,
            options: [
                {
                    label: '普通审批',
                    value: 1
                },
                {
                    label: '加急审批',
                    value: 2
                }
            ],
            overtimeType: 1
        },
        created() {
            for(var i = 1;i <= 60;i++) {
                this.during.slots[0].values.push({name:i*0.5,id:i});
            }
            if(window.location.href.indexOf('id') > -1) {
                var hrefs = window.location.href.split("/"),
                    ids = hrefs[hrefs.length - 1].split("."),
                    _this = this;
                _this.id = ids[0];
                _this.$indicator.open({
                    text: '正在获取中',
                    spinnerType: 'triple-bounce'
                });
                _this.$http.post('{:Url("Approval/getDetail")}',{
                    'id': _this.id,
                    'class': _this.type
                },{emulateJSON:true}).then(function (res) {
                    var data = res.body;
                    console.log(data)
                    _this.$indicator.close();
                    if(data.code == 1){
                        _this.startTime = data.data.start_time;
                        _this.endTime = data.data.end_time;
                        _this.remark = data.data.content;
                        _this.overtimeType = data.data.type;
                        Object.assign(_this.approvalUser,data.data.approval_user);
                        _this.during.nowType.name = data.data.duration;
                        _this.during.nowType.id = parseInt(data.data.type) / .5;
                        _this.approver.nowType.userid = data.data.apply_user == data.data.approval_user[0].approval_user?data.data.approval_user[1].approval_user:data.data.approval_user[0].approval_user;
                        _this.approver.nowType.username = data.data.apply_user == data.data.approval_user[0].approval_user?data.data.approval_user[1].username:data.data.approval_user[0].username;
                    }else{
                        _this.$toast({
                            message: data.msg,
                            duration: 1500
                        });
                    }
                }, function (res) {
                    _this.$indicator.close();
                    _this.$toast({
                        message: "获取失败，请重试！",
                        duration: 1500
                    });
                });
            }
            this.getApprovalUser();
        },
        mounted() {
            var _this = this;
            _this.$nextTick(function() {
                setTimeout(function() {
                    _this.approver.slots[0].defaultIndex = 0;
                    _this.during.slots[0].defaultIndex = 0;
                }, 100);
            });
        },
        watch:{
            overtimeType() {
                this.getApprovalUser();
            }
        },
        methods: {
            getApprovalUser() {
                var _this = this;
                _this.$indicator.open({
                    text: '正在获取中',
                    spinnerType: 'triple-bounce'
                });
                _this.$http.post('{:Url("Approval/getOvertimeApprovalUser")}',{
                    'type': _this.overtimeType
                },{emulateJSON:true}).then(function (res) {
                    var data = res.body;
                    console.log(data);
                    _this.$indicator.close();
                    if(data.code == 1){
                        this.approver.slots[0].values = data.data;
                    }else{
                        _this.$toast({
                            message: data.msg,
                            duration: 1500
                        });
                    }
                }, function (res) {
                    _this.$indicator.close();
                    _this.$toast({
                        message: "获取失败，请重试！",
                        duration: 1500
                    });
                });
            },
            checkDuring() {
                if(parseFloat(this.during) % 0.5 > 0) {
                    this.$toast({
                        message:"请填写0.5的倍数",
                        duration:1500
                    });
                    this.during = '';
                }
            },
            chooseStartTime() {
                this.$refs.timePicker_1.open();
            },
            chooseEndTime() {
                this.$refs.timePicker_2.open();
            },
            toggleDuring(){
                var _this = this;
                _this.during.popupVisible = !_this.during.popupVisible;
            },
            duringConfirm(){
                var _this = this;
                if(_this.during.nowType != _this.during.onChangeType){
                    _this.during.nowType = _this.during.onChangeType;
                }
                _this.during.popupVisible = !_this.during.popupVisible;
            },
            selectDuring(p,v){
                var _this = this;
                if(v[0]){
                    _this.during.onChangeType = v[0];
                }
            },
            toggleApprover(){
                var _this = this;
                _this.approver.popupVisible = !_this.approver.popupVisible;
            },
            approverConfirm(){
                var _this = this;
                console.log(_this.approvalUser)
                if(_this.approver.nowType != _this.approver.onChangeType){
                    _this.approver.nowType = _this.approver.onChangeType;
                    if(_this.id != 0) {
                        _this.approvalUser[0].username = _this.approver.nowType.username;
                        _this.approvalUser[0].approval_user = _this.approver.nowType.userid;
                    }
                }
                _this.approver.popupVisible = !_this.approver.popupVisible;
            },
            selectApprover(p,v){
                var _this = this;
                if(v[0]){
                    _this.approver.onChangeType = v[0];
                }
            },
            startTimeConfirm(val) {
                this.start_time = val;
                this.startTime = new Date(this.start_time).getTime() / 1000;
            },
            endTimeConfirm(val) {
                this.end_time = val;
                this.endTime = new Date(this.end_time).getTime() / 1000;
            },
            cancel(){
                window.history.go(-1)
            },
            subimtAjax(){
                var _this =this;
                if(!_this.clickLoading) {
                    return false;
                }
                _this.$messagebox.confirm('是否确认提交','在线审批').then(function () {
                    if(typeof _this.startTime != 'number' || typeof _this.endTime != 'number'){
                        _this.$toast({
                            message:"请选择申请时间",
                            duration:1500
                        });
                        return false;
                    }
                    if(_this.during.nowType.id == 0){
                        _this.$toast({
                            message:"请填写加班时长",
                            duration:1500
                        });
                        return false;
                    }
                    if(_this.approver.nowType.userid == 0){
                        _this.$toast({
                            message:"请选择审批人",
                            duration:1500
                        });
                        return false;
                    }
                    if(!_this.remark){
                        _this.$toast({
                            message:"请输入加班理由",
                            duration:1500
                        });
                        return false;
                    }
                    _this.clickLoading = false;
                    _this.$indicator.open({
                        text: '正在发布中',
                        spinnerType: 'triple-bounce'
                    });
                    var data = {
                        'class':_this.type,
                        'start_time':_this.startTime,
                        'end_time':_this.endTime,
                        'duration':_this.during.nowType.name,
                        'content':_this.remark,
                        'type': _this.overtimeType
                    };
                    if(_this.id != 0) {
                        var url = '{:Url("Approval/edit")}';
                        data.id = _this.id;
                        var obj = {
                            'content': data,
                            'approval_user': _this.approver.nowType.userid
                        }
                    } else {
                        var url = '{:Url("Approval/overtime")}',
                            obj = data;
                        obj.approval_user = _this.approver.nowType.userid;
                    }
                    console.log(data)
                    _this.$http.post(url,obj,{emulateJSON:true}).then(function (res) {
                        var data = res.body;
                        console.log(data)
                        _this.$indicator.close();
                        if(data.code == 1){
                            _this.$toast({message:'提交成功',iconClass: 'icon', duration:1500});
                            window.sessionStorage.setItem('approval_type','2');
                            setTimeout(function () {
                                if(_this.id != 0) {
                                    window.location.replace('/home/approval/index.html');
                                    window.sessionStorage.setItem('approval_type','2');
                                } else {
                                    window.history.go(-1);
                                }
                            },500);
                        }else{
                            _this.clickLoading = true;
                            _this.$toast({
                                message: data.msg,
                                duration: 1500
                            });
                        }
                    }, function (res) {
                        _this.$indicator.close();
                        _this.$toast({
                            message: "提交失败，请重试！",
                            duration: 1500
                        });
                    });
                });

            }
        }
    })
</script>
{/block}
